package com.android.lovingpet.mine.pet.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.bean.Pet;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ikata on 2018/4/29.
 */

public class PetRvAdapter extends RecyclerView.Adapter<PetRvAdapter.viewHolder> implements View.OnClickListener {
    private List<Pet> list;
    private Context context;
    private OnItemClickListener onItemClickListener;
    public PetRvAdapter()
    {
        this.list=new ArrayList<>();
    }
    public PetRvAdapter(List<Pet> list)
    {
        this.list=list;
    }
    public Pet getPet(int position)
    {
        return list.get(position);
    }

    public void addPet(Pet pet)
    {
        list.add(pet);
        notifyItemInserted(list.size());
        notifyItemChanged(list.size());
    }
    public void addPet(ArrayList<Pet> list)
    {
        this.list=list;
        notifyDataSetChanged();
    }
    @Override
    public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_pet,parent,false);
        viewHolder viewHolder=new viewHolder(view);
        view.setOnClickListener(this);
        context=parent.getContext();
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(viewHolder holder, int position) {
        holder.tv_name.setText(list.get(position).getName());
        holder.tv_sex.setText(list.get(position).getGender());
        holder.tv_type.setText(list.get(position).getType());
        Glide.with(context)
                .load(list.get(position).getImage())
                .into(holder.iv_head);
        holder.itemView.setTag(position);
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener!=null)
        {
            onItemClickListener.onItemClick((Integer)v.getTag());
        }
    }

    class viewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_head;
        private TextView tv_name;
        private TextView tv_sex;
        private TextView tv_type;
        public viewHolder(View itemView) {
            super(itemView);
            iv_head=itemView.findViewById(R.id.iv_head);
            tv_name=itemView.findViewById(R.id.tv_name);
            tv_sex=itemView.findViewById(R.id.tv_sex);
            tv_type=itemView.findViewById(R.id.tv_type);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public void setItemClickListener(OnItemClickListener itemClickListener)
    {
        onItemClickListener=itemClickListener;
    }

}
